using Admin.Application.Dto.Department;
using Admin.Application.Repositories;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Admin.Application.Dto;

namespace Admin.Api.Controllers;

[Authorize]
[ApiController]
[Route("api/[controller]")]
public class DepartmentsController(IDepartmentService departmentService) : ControllerBase
{
    private readonly IDepartmentService _departmentService = departmentService;

    [HttpGet("list")]
    public async Task<IActionResult> GetAll(int pageIndex = 0, int pageSize = 10)
    {
        var departments = await _departmentService.GetPagedDepartmentAsync(pageIndex, pageSize);
        return Ok(departments);
    }

    [HttpGet("get/{id}")]
    public async Task<IActionResult> GetById(Guid id)
    {
        var department = await _departmentService.GetDepartmentByIdAsync(id);
        return Ok(department);
    }

    [HttpPost("add")]
    public async Task<IActionResult> Add(DepartmentInfoDto departmentDto)
    {
        var result = await _departmentService.AddDepartmentAsync(departmentDto);
        return Ok(result);
    }

    [HttpPut("update/manager/{departmentId}/{managerID?}")]
    public async Task<IActionResult> UpdateManager(Guid departmentId, Guid? managerID)
    {
        var result = await _departmentService.UpdateDepartmentManagerAsync(departmentId, managerID);
        return Ok(result);
    }


    [HttpPut("update/parent/{departmentId}/{parentId?}")]
    public async Task<IActionResult> UpdateParent(Guid departmentId, Guid? parentId)
    {
        var result = await _departmentService.UpdateDepartmentParentAsync(departmentId, parentId);
        return Ok(result);
    }

    [HttpPut("update/info/{id}")]
    public async Task<IActionResult> UpdateInfo(Guid id, DepartmentInfoDto departmentDto)
    {
        var result = await _departmentService.UpdateDepartmentInfoAsync(id, departmentDto);
        return Ok(result);
    }

    [HttpPut("update/isactive/{id}")]
    public async Task<IActionResult> UpdateIsActive(Guid id,  ActiveDto activeDto)
    {
        var result = await _departmentService.UpdateIsActive(id, activeDto);
        return Ok(result);
    }

    [HttpDelete("delete/{id}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        var result = await _departmentService.DeleteDepartmentAsync(id);
        return Ok(result);
    }
}